package com.yidu.BusinessParament.service.impl;

import com.yidu.BusinessParament.entity.Bond;
import com.yidu.BusinessParament.mapper.BondMapper;
import com.yidu.BusinessParament.service.BondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周楠櫻木
 * @version 1.0
 * @date 2022/12/6
 */
@Service
@Transactional
public class BondServiceImpl implements BondService {
    @Resource
    @Autowired
    BondMapper bondMapper;

    /**
     * 查询遍历
     * @param bond
     * @param page
     * @param limit
     * @return
     */
    public List<Bond> selectBond(Bond bond, int page, int limit) {
        Map map = new HashMap();
        String sqlWhere = getWhere(bond);
        map.put("sqlWhere", sqlWhere);
        map.put("page",(page-1)*limit);
        map.put("limit",limit);

        return bondMapper.selectBond(map);
    }

    /**
     * 总页数
     * @param bond
     * @return
     */
    public int getSumRow(Bond bond) {
        Map map = new HashMap();
        String sqlWhere = getWhere(bond);
        map.put("sqlWhere", sqlWhere);
        return bondMapper.getSumRow(map);
    }

    /**
     * 新增
     * @param bond
     * @return
     */
    public int insertBond(Bond bond) {
        return bondMapper.insertBond(bond);
    }

    /**
     * 删除
     * @param bondId
     * @return
     */
    public int deleteBond(String bondId) {
        String[] split=bondId.split(",");
        ArrayList<Object> arrayList=new ArrayList<Object>();
        for (String id :split){
            arrayList.add(id);
        }
        return bondMapper.deleteBond(arrayList);
    }

    /**
     * 修改
     * @param bond
     * @return
     */
    public int updateBond(Bond bond) {
        int i = bondMapper.updateBond(bond);
        return i;
    }

    /**
     * 查询拼接
     * @param bond
     * @return
     */
    public String getWhere(Bond bond) {
        StringBuilder stringBuilder = new StringBuilder();
        if (bond.getBondId()!=null && !bond.getBondId().equals("")) {
            stringBuilder.append(" and bondId LIKE '%"+bond.getBondId()+"%'");
        }
        if(bond.getBearingStart()!=null) {
            stringBuilder.append(" and bearingStart LIKE '%"+bond.getBearingStart()+"%'");
        }
        return stringBuilder.toString();
    }


}
